package servlets;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import control.ControlService;


public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request,response);
	}

	private void processRequest(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String username = request.getParameter("username");
		String password = request.getParameter("password");		
		String requestXML = "<param name='username' value='"+username+"' />";
		requestXML +="<param name='password' value='"+password+"' />";
		
		
		String responseXML = new ControlService().Call("loginuser",requestXML);
		try {			
			DocumentBuilderFactory documentFactory=DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;
			builder = documentFactory.newDocumentBuilder();
			Document doc = builder.parse(new ByteArrayInputStream(responseXML.getBytes("utf8")));
			NodeList responseNodeList = doc.getElementsByTagName("response");
			Element status = (Element)responseNodeList.item(0);
			if (status.getAttribute("name").equals("success")){
				String userid = ((Element)responseNodeList.item(1)).getAttribute("value");
				String identity = ((Element)responseNodeList.item(2)).getAttribute("value");
				request.getSession().setAttribute("userID", userid);
				request.getSession().setAttribute("identity", identity);
				request.getSession().setAttribute("userName", username);
				
				if (identity.equals("ADMIN")){
					response.sendRedirect("../coursemanage.jsp");
				}else{
					response.sendRedirect("../home.jsp");
				}
			}else {
				request.getSession().setAttribute("errorMsg", "登录失败："+status.getAttribute("value"));
				response.sendRedirect("../login.jsp");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	@Override
	public void init() throws ServletException {
		// Put your code here
	}

}
